//
//  LJStatusCell.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/22.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit

class LJStatusCell: UITableViewCell {

   /// MARK: - 属性
    /// 模型属性
    var status: LJStatus?{
        didSet{
            topView.status = status
            
            contentLabel.text = status?.text
            
            pictureView.status = status
            
            // 设置图片大小
//            print("配图的大小\(pictureView.calcViewSize())")
            pictureViewHeightCons?.constant = pictureView.calcViewSize().height
            pictureViewWidthCons?.constant = pictureView.calcViewSize().width
            
            bottomView.forwardButton.setTitle("\(status?.reposts_count ?? 0)", forState: UIControlState.Normal)
            bottomView.commentButton.setTitle("\(status?.comments_count ?? 0)", forState: UIControlState.Normal)
            bottomView.zanButton.setTitle("\(status?.attitudes_count ?? 0)", forState: UIControlState.Normal)
        }
    }
    
    /// 图片宽度约束
    var pictureViewWidthCons: NSLayoutConstraint?
    var pictureViewHeightCons: NSLayoutConstraint?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 准备UI
        prepareUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI() {
        // 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        // 添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 52))
        
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: -2 * 8))
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
    }
    
    /// 计算cell行高的方法
    func rowHeight(status: LJStatus) -> CGFloat {
        // 赋值同时会给控件布局
        self.status = status
        // 更新布局
        layoutIfNeeded()
        
        let maxY = CGRectGetMaxY(bottomView.frame)
        return maxY
    }
    
    /// MARK: - 懒加载
    /// 顶部视图
    lazy var topView: LJTopView = LJTopView()
    
    /// 内容文本
    lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        label.numberOfLines = 0
        
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * 8
        return label
    }()
    
    /// 图片
    lazy var pictureView: LJStatusPictureView = LJStatusPictureView()
    
    /// 底部视图
    lazy var bottomView: LJBottomView = LJBottomView()
    
    
}
